<?php
namespace BackHome\Controller;
use Think\Controller;
class VillageController extends CommonController {
    public function index(){
		$village=D('village');
		//原生态写法
		$Model = M();
		$sql="SELECT (select name from `tv_area` where id=v.area_lev_1)as a1,(select name from `tv_area` where id=v.area_lev_2)as a2,(select name from `tv_area` where id=v.area_lev_3)as a3,(select name from `tv_area` where id=v.area_lev_4)as a4,(select name from `tv_area` where id=v.area_lev_5)as a5,v.* FROM `tv_village` v WHERE 1 order by id desc";
		$getall=$Model->query($sql);
		
		$this->assign('getall',$getall);
        $this->display();
    }//end fun
    public function add(){
		$area=D('area');
		$getfirst=$area->where("`re_id` =0")->select();
		$this->assign('areali',$getfirst);
        $this->display();
    }//end fun
	
	public function ins(){
		$village=D('village');
		$data=$_POST;
		$data['addtime']=time();
		$data['uptime']=time();
		$alt=$village->add($data);
		if($alt>0){
			$this->ajaxReturn("ok");
			}else{
				$this->ajaxReturn("err");
			}
	}//end fun
	
	public function edit(){
		$area=D('area');
		$getfirst=$area->where("`re_id` =0")->select();
		$this->assign('areali',$getfirst);
		//原生态写法
		$Model = M();
		$sql="SELECT (select name from `tv_area` where id=v.area_lev_1)as a1,(select name from `tv_area` where id=v.area_lev_2)as a2,(select name from `tv_area` where id=v.area_lev_3)as a3,(select name from `tv_area` where id=v.area_lev_4)as a4,(select name from `tv_area` where id=v.area_lev_5)as a5,v.* FROM `tv_village` v WHERE v.id=".$_GET['id']." limit 0,1 ";
		$getinfo=$Model->query($sql);//dump($getinfo);
		//图片分解为数组
		$imgarr=explode(',',$getinfo[0]['imgs']);
		$getinfo[0]['img1']=$imgarr[0];
		$getinfo[0]['img2']=$imgarr[1];
		$getinfo[0]['img3']=$imgarr[2];
		$getinfo[0]['img4']=$imgarr[3];
		$getinfo[0]['img5']=$imgarr[4];
		$this->assign('getinfo',$getinfo);
        $this->display();
	}//end fun 
	
	public function upd(){
		$village=D('village');
		$data=$_POST;
		$data['uptime']=time();
					//处理多余的逗号		
					$data['imgs']=preg_replace('/(,)+/i',',',$_POST['imgs']);
					$data['imgs']=rtrim($data['imgs'],',');//去尾
					$post_group['imgs']=ltrim($data['imgs'],',');//去头
		$alt=$village->save($data);
		if($alt>0){
			$this->ajaxReturn("ok");
			}else{
				$this->ajaxReturn("err");
			}
	}//end
	
}